package com.moutai.wmo.core.security.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moutai.wmo.core.security.dao.UserDao;
import com.moutai.wmo.core.security.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * <a href="https://springdoc.cn/spring-security/servlet/authentication/passwords/user-details-service.html">关于：UserDetailsService</a>
 */
@Slf4j
@Service
@Component
public class LoginUserDetailService implements UserDetailsService {

    private final UserDao userDao;

    @Autowired
    public LoginUserDetailService(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("通过用户名({})查找用户，获取UserDetails对象（含内部的成员属性 password）", username);
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, username);
        User user = userDao.selectOne(lqw);// 从数据库中查询用户数据
        log.info("查询用户数据：{}", user);
        return user;
    }
}
